/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import net.worlds.input.InputState;
import net.worlds.net.ChatMessage;
import net.worlds.object.Tool;
import net.worlds.ui.UI;
import net.worlds.ui.edge.EMain;
import net.worlds.ui.edge.GameOverlay;

/**
 *
 * @author Jacob
 */
public class GameView extends MainView{
    private int bloodAlpha = 0;
    private Color bloodColor;
    public GameView()
    {
        overlayUI = new GameOverlay(width, height);
        Main.loading = false;
    }
    protected void drawGameUI(Graphics2D g, World world, int viewWidth, int viewHeight, float worldWidth, float worldHeight)
    {
        if(bloodAlpha > 0)
        {
            g.setColor(new Color(128, 0, 0, bloodAlpha));
            g.fillRect(0, 0, width, height);
        }
        if(!Main.loading)
        {
            if(Main.getLocalPlayer() != null)
            {
                LocalPlayer player = Main.getLocalPlayer();
                int startX = 25;
                int i = 1;
                for(Tool t : player.getTools())
                {
                    BufferedImage img = t.getImage();
                    if(img == null)
                        continue;
                    int imgWidth = img.getWidth();
                    int imgHeight = img.getHeight();
                    if(t == player.getSelectedTool())
                        g.setColor(selectedToolColor);
                    else
                        g.setColor(defaultToolColor);
                    String n = t.toString();
                    int rectWidth = imgWidth;
                    Rectangle2D bounds = g.getFontMetrics().getStringBounds(n, g);
                    if(bounds.getWidth() > imgWidth)
                        rectWidth = (int) bounds.getWidth();
                    int yOffset = 0;
                    g.fillRoundRect(startX, 10 + yOffset, rectWidth + 40, imgHeight + 40 + 10, 10, 10);
                    g.drawImage(img, startX + 20, 40 + yOffset, null);
                    g.setColor(Color.white);
                    g.drawString(n, startX + 20, imgHeight + 55 + yOffset);
                    g.drawString(Integer.toString(i), startX + 4, 45 + yOffset);
                    startX += rectWidth + 40 + 10;
                    i++;
                }
            }
            if(Main.getPlayer() != null && Main.getPlayer().getMessage() != null)
            {
                String m = Main.getPlayer().getMessage();
                g.setFont(messageFont);
                Rectangle2D rect = g.getFontMetrics().getStringBounds(m, g);
                g.setColor(selectionRectBackgroundColor);
                g.fillRect(0, viewHeight - 50 - 15, viewWidth, 50);
                int textX = (int) (viewWidth / 2 - rect.getWidth() / 2);
                int textY = viewHeight - 15 - 15;
                g.setColor(Color.black);
                g.drawString(m, textX + 1, textY + 1);
                g.setColor(Color.white);
                g.drawString(m, textX, textY);
            }

            if(AssetLoader.getInstance().isBusy())
            {
                int astartX = 5;
                int startY = height - 20;
                g.setColor(defaultToolColor);
                g.fillRect(astartX, startY, 50, 15);
                int[] data = AssetLoader.getInstance().getInfo();
                double percent;
                if(data[0] == 0)
                    percent = 0;
                else
                    percent = (double)data[1]/data[0];
                int barWidth = (int) (50 * percent);
                g.setColor(selectedToolColor);
                g.fillRect(astartX, startY, barWidth, 15);
            }
        }
        else
        {
            ((Graphics2D)g).setPaint(new GradientPaint(0,0, c1, viewWidth, viewHeight, c2));
            g.fillRect(0, 0, width, height);
        }
        if(world.getMessage() != null)
        {
            String m = world.getMessage();
            g.setFont(messageFont);
            Rectangle2D rect = g.getFontMetrics().getStringBounds(m, g);
            g.setColor(selectionRectBackgroundColor);
            g.fillRect(0, 0, viewWidth, viewHeight);
            int textX = (int) (viewWidth/ 2 - rect.getWidth() / 2);
            int textY = (int) (viewHeight / 2 + rect.getHeight() / 2);
            g.setColor(Color.black);
            g.drawString(m, textX + 1, textY + 1);
            g.setColor(Color.white);
            g.drawString(m, textX, textY);
        }
        ChatMessage[] chats = Chat.getChats();
        int chatStartY = height/2;
        int chatY = chatStartY;
        for(int i = chats.length - 1; i > -1; i--)
        {
            if(chats[i] != null && chats[i].getAge() < 60 * 1000 || !Main.chatBarText.equals("Press . to chat"))
            {
                String m = chats[i].getMessage();
                g.setFont(defaultFont);
                g.setColor(transparentBlack);
                g.drawString(m, 41, chatY + 1);
                g.setColor(chats[i].getColor());
                g.drawString(m, 40, chatY);
                chatY -= 18;
            }
            else
                break;
        }
        if(Main.getLocalPlayer() != null)
        {
            g.setColor(Color.black);
            g.fillRect(0, height - 16, width, 16);
            g.setColor(Color.white);
            g.setFont(defaultFont);
            if(Main.chatBarText.equals("Press . to chat"))
                g.drawString(Main.chatBarText, 5, height - 3);
            else
                g.drawString(Main.chatBarText+"_", 5, height - 3);
        }
        if(Stats.showStats)
        {
            int y = 100;
            drawText(g, "FPS: "+Stats.fps, 50, y);
            y += 20;
            drawText(g, "# Bodies: "+Stats.numBodies, 50, y);
            y += 20;
            drawText(g, "# Pairs: "+Stats.numPairs, 50, y);
            y += 20;
            drawText(g, "# Proxies: "+Stats.numProxies, 50, y);
            y += 20;
            drawText(g, "# Contacts: "+Stats.numContacts, 50, y);
            y += 20;
            drawText(g, "# Joints: "+Stats.numJoints, 50, y);
            y += 20;
            drawText(g, "# Objects: "+Stats.numObjects, 50, y);
            y += 20;
            drawText(g, "Task Queue Size: "+Stats.taskQueueSize, 50, y);

        }
        BufferedImage mouse = UI.getMouseImage();
        if(mouse != null && UI.mouseInside)
        {
            if(UI.cursor == UI.crosshairCursor)
                g.drawImage(mouse, InputState.mouseX + 10, InputState.mouseY + 8, null);
            else
                g.drawImage(mouse, InputState.mouseX + 15, InputState.mouseY + 8, null);
        }
    }

    @Override
    public EMain getOverlay() {
        return overlayUI;
    }

    @Override
    public EMain getTop() {
        return null;
    }

    public boolean eMouseDown(int b, int x, int y)
    {
        if(getOverlay().eMouseDown(b, x, y))
            return true;
        else
            return false;
    }
    public boolean eMouseUp(int b, int x, int y)
    {
        if(getOverlay().eMouseUp(b, x, y))
            return true;
        else
            return false;
    }
    public boolean eMouseMove(int x, int y)
    {
        if(getOverlay().eMouseMove(x, y))
            return true;
        else
            return false;
    }
    public boolean eMouseDrag(int b, int x, int y)
    {
        if(getOverlay().eMouseDrag(b, x, y))
            return true;
        else
            return false;
    }
    @Override
    public void setSize(int x, int y)
    {
        super.setSize(x, y);
        overlayUI.setSize(x, y);
    }
    public void hurt()
    {
        bloodAlpha = 128;
        Main.getScriptContext().run(new Runnable()
        {

            public void run() {
                bloodAlpha -= 16;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                }
                if(bloodAlpha > 0)
                    run();
            }
            
        });
    }
}
